const fp = require('lodash/fp');
const cars = require('./question2');

/*
* 练习3: 使用帮助函数_average重构averageDollarValue,使用函数组合的方式实现
*let _average = function(xs) {
  return fp.reduce(fp.add, 0 , xs) / xs.length
};

let averageDollarValue = function (cars) {
  let dollar_values = fp.map(function (car) {
    return car.dollar_value
  }, cars)
  return _average(dollar_values);
};
*
*  */

let _average = function(xs) {
  return fp.reduce(fp.add, 0 , xs) / xs.length
};

// map返回带金额的数组，使用箭头函数简写

// Point Free
let getDollarValues = fp.map(car => car.dollar_value);

// 使用flowRight组合函数
let averageDollarValue = fp.flowRight(_average, getDollarValues);

console.log(averageDollarValue(cars));




